<?php
/**
 * Created by PhpStorm.
 * User: xpz
 * Date: 2021/5/20
 * Time: 13:11
 */
namespace app\admin\controller\marketing;

use app\common\controller\Backend;
use think\Db;

class Share extends Backend
{
    protected $model = null;
    public function _initialize()
    {
        parent::_initialize();
        $this->model = model('Company');
    }

    public function index(){
        //设置过滤方法
        $this->request->filter(['strip_tags']);
        if ($this->request->isAjax()) {
            //如果发送的来源是Selectpage，则转发到Selectpage
            if ($this->request->request('keyField')) {
                return $this->selectpage();
            }
            list($where, $sort, $order, $offset, $limit) = $this->buildparams('color');
            $total = $this->model
                ->where($where)
                ->order($sort, $order)
                ->count();
            $list = $this->model
                ->where($where)
                ->order($sort, $order)
                ->limit($offset, $limit)
                ->select();
            $result = array("total" => $total, "rows" => $list);


            return json($result);
        }
        return $this->view->fetch();
    }

    public function edit($ids = null)
    {
        $row = $this->model->get($ids);
        if (!$row) {
            $this->error(__('No Results were found'));
        }
        $adminIds = $this->getDataLimitAdminIds();
        if (is_array($adminIds)) {
            if (!in_array($row[$this->dataLimitField], $adminIds)) {
                $this->error(__('You have no permission'));
            }
        }
        if ($this->request->isPost()) {
            $params = $this->request->post("row/a");
            if ($params) {
                $params = $this->preExcludeFields($params);
                $params['douyin'] = $this->_dykl($params['douyin']);
//                print_r($params);exit;
                $params['kuaishou'] = $this->_kskl($params['kuaishou']);
                $params['taobao'] = $this->_taobaokl($params['taobao']);
                $params['tianmao'] = $this->_tianmaokl($params['tianmao']);
                $params['albb'] = $this->_albbkl($params['albb']);
//                print_r($params['douyin']);exit;
                $result = false;
                Db::startTrans();
                try {
                    //是否采用模型验证
                    if ($this->modelValidate) {
                        $name = str_replace("\\model\\", "\\validate\\", get_class($this->model));
                        $validate = is_bool($this->modelValidate) ? ($this->modelSceneValidate ? $name . '.edit' : $name) : $this->modelValidate;
                        $row->validateFailException(true)->validate($validate);
                    }
                    $result = $row->allowField(true)->save($params);
                    Db::commit();
                } catch (ValidateException $e) {
                    Db::rollback();
                    $this->error($e->getMessage());
                } catch (PDOException $e) {
                    Db::rollback();
                    $this->error($e->getMessage());
                } catch (Exception $e) {
                    Db::rollback();
                    $this->error($e->getMessage());
                }
                if ($result !== false) {
                    $this->success();
                } else {
                    $this->error(__('No rows were updated'));
                }
            }
            $this->error(__('Parameter %s can not be empty', ''));
        }
        $this->view->assign("row", $row);
        return $this->view->fetch();
    }

    private function _dykl($str){
        if(empty($str)){
            return "";
        }

        preg_match_all('/[0-9a-zA-Z]+/',$str,$match);
//        print_r($match);exit;
        if(!empty($match[0])){
            $num = 3;
            foreach($match[0] as $k=>$v){
                if($v == 'com'){
                    $num = $k;
                }
            }
            $num++;
//            if(count($match[0]) == 5){
            return "https://v.douyin.com/".$match[0][$num]."/";
//            }
        }
        return "";
//        return trim(cut("com/","/",$str));
//        return "##".trim(cut("##", "##", $str),"##")."##";
    }

    private function _kskl($str){
        if(empty($str)){
            return "";
        }

        preg_match_all('/[0-9a-zA-Z]+/',$str,$match);
//        print_r($match);exit;
        if(!empty($match[0])){
            $num = 3;
            foreach($match[0] as $k=>$v){
                if($v == 'com'){
                    $num = $k;
                }
            }
            $num++;
//            if(count($match[0]) == 5){
            return " https://v.kuaishou.com/".$match[0][$num]."/";
//            }
        }
        return "";
//        return trim(cut("com/","/",$str));
//        return "##".trim(cut("##", "##", $str),"##")."##";
    }
    private function _taobaokl($str){
        if(empty($str)){
            return "";
        }
//       echo $str;
//       var_dump(substr($str, 0,30));exit;
        return substr($str, 0,30);
//        return trim(cut("com/","/",$str));
//        return "##".trim(cut("##", "##", $str),"##")."##";
    }
    private function _tianmaokl($str){
        if(empty($str)){
            return "";
        }
//       echo $str;
//       var_dump(substr($str, 0,30));exit;
        return substr($str, 0,32);
//        return trim(cut("com/","/",$str));
//        return "##".trim(cut("##", "##", $str),"##")."##";
    }
    private function _albbkl($str){
        if(empty($str)){
            return "";
        }
        return "复制￥".Tool::get_between($str, '复制￥', '打开【手机阿里】');
    }
}